package com.yilin.platform.controller.system;

import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.annotation.NotNull;
import com.yilin.platform.base_core.commons.annotation.UserTarget;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.entity.enums.user.UserType;
import com.yilin.platform.entity.system.Jurisdiction;
import com.yilin.platform.service.system.IJurisdictionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能API管理
 */
@RestController
@RequestMapping("/system/jurisdiction/")
public class JurisdictionController extends BaseController {

    @Resource
    IJurisdictionService jurisdictionService;

    /**
     * 获取权限List
     */
    @PostMapping("getAllJurisdictionList")
    public ReturnData<List<Jurisdiction>> getAllJurisdictionList() {
        List<Jurisdiction> jurisdictionList = jurisdictionService.getJurisdictionList();
        return jsonView(true, "获取列表成功", jurisdictionList);
    }

    /**
     * 获取权限List
     */
    @PostMapping("getJurisdictionListByIds")
    public ReturnData<List<Jurisdiction>> getJurisdictionListByIds(String[] jurisdictionIds) {
        List<Jurisdiction> jurisdictionList = jurisdictionService.getJurisdictionListByIds(jurisdictionIds);
        return jsonView(true, "获取列表成功", jurisdictionList);
    }

    @PostMapping("getJurisdictionPage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<Jurisdiction>> getJurisdictionPage(Jurisdiction query, Integer page, Integer limit) {
        PageData<Jurisdiction> pageData = jurisdictionService.getJurisdictionPage(query,page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 添加权限
     */
    @PostMapping("saveJurisdiction")
    @UserTarget({UserType.adminUser})
    public ReturnData<Object> saveJurisdiction(Jurisdiction jurisdiction) {
        ParamUtil.isNull(getLoginUser(), "当前登录用户为空");
        ParamUtil.isNull(jurisdiction, "权限为空");
        ParamUtil.isNull(jurisdiction.getCode(), "权限代码为空");
        ParamUtil.isNull(jurisdiction.getRemark(), "权限说明为空");
        jurisdiction.setAddUserId(getLoginUser().getId());
        jurisdictionService.saveJurisdiction(jurisdiction);
        return jsonView(true, "保存成功");
    }
    /**
     * 编辑权限
     */
    @PostMapping("editJurisdiction")
    @UserTarget({UserType.adminUser})
    public ReturnData<Object> editJurisdiction(Jurisdiction jurisdiction) {
        ParamUtil.isNull(jurisdiction.getId(), "未指定权限代码");
        jurisdictionService.editJurisdiction(jurisdiction);
        return jsonView(true, "删除成功");
    }

    /**
     * 删除权限
     */
    @PostMapping("removeJurisdiction")
    @UserTarget({UserType.adminUser})
    public ReturnData<Object> removeJurisdiction(Jurisdiction jurisdiction) {
        ParamUtil.isNull(jurisdiction.getId(), "未指定权限代码");
        String[] jurisdictionIds = new String[]{jurisdiction.getId()};
        jurisdictionService.removeJurisdiction(jurisdictionIds);
        return jsonView(true, "删除成功");
    }

}
